<template>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
  <div class="container">
    <a class="navbar-brand" href="#">我的博客</a>
    
    <div class="collapse navbar-collapse" id="navbarText">
      <ul class="navbar-nav me-auto mb-2 mb-lg-0">
        <li class="nav-item">
          <router-link :class="route_name === 'home' ? 'nav-link active' : 'nav-link'" :to="{name: 'home'}">首页</router-link>
        </li>
        <li class="nav-item">
          <router-link :class="route_name === 'userlist' ? 'nav-link active' : 'nav-link'" :to="{name: 'userlist'}">用户列表</router-link>
        </li>
        <li class="nav-item">
          <router-link :class="route_name === 'userprofile' ? 'nav-link active' : 'nav-link'" :to="{name: 'userprofile', params: {userId: 2}}">个人动态</router-link>
        </li>
      </ul>
      <ul class="navbar-nav" v-if="!$store.state.user.is_login">
        <li class="nav-item">
          <router-link :class="route_name === 'login' ? 'nav-link active' : 'nav-link'" :to="{name: 'login'}">登录</router-link>
        </li>
        <li class="nav-item">
          <router-link :class="route_name === 'register' ? 'nav-link active' : 'nav-link'" :to="{name: 'register'}">注册</router-link>
        </li>
      </ul>

      <ul class="navbar-nav" v-else>
        <li class="nav-item">
          <router-link :class="route_name === 'login' ? 'nav-link active' : 'nav-link'" :to="{name: 'userprofile', params: {userId: $store.state.user.id}}">{{ $store.state.user.username }}</router-link>
        </li>
        <li class="nav-item">
          <router-link :class="route_name === 'register' ? 'nav-link active' : 'nav-link'" :to="{name: 'login'}" @click="logout">退出</router-link>
        </li>
      </ul>
    </div>
  </div>
</nav>
</template>

<script>
import { useRoute } from 'vue-router';
import { computed } from 'vue';
import { useStore } from 'vuex';

export default {
    name:"NavBar",
    setup(){
      const store = useStore();
      const route = useRoute();
      let route_name = computed(() => route.name);

      const logout = () => {
        store.dispatch("logout");
      }
      return {
          route_name,
          logout,
      }
    },

    
    
}
</script>

<style scoped>

</style>
